package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ManipulateSubSysMatainer extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ManipulateSubSysMatainer() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request,response);
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/text");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String errPars = "}";
		String strSQL = null;
		
		/*	
		 * 1 ==> 新增
		 * 2 ==> 刪除
		 * */
		int actType = Integer.parseInt( request.getParameter("actType") );
		
		int roleType = Integer.parseInt( request.getParameter("roleType") );
		
		final String bssId = "MBMS";
		String subSysId = request.getParameter("subSysId");
		String[] list = null;
		String res = "";
		
		try {
			
			if ( actType ==2 ) {
				
			}
			conn = wid.getConn(true);	
			
			
			if ( actType ==2 ) {	//刪除指定的
				strSQL = "DELETE FROM mproleuser WHERE bssid=? AND subsysid=? AND roleid=? AND userid=?";
				
				ps = conn.prepareStatement(strSQL);
				ps.setString(1, bssId);
				ps.setString(2, subSysId);
				ps.setInt(3, roleType);
				
				list = request.getParameter("delList").split(wid.splitTokenONE);
				
				if ( !request.getParameter("delList").contains(wid.splitTokenONE) ) {
					ps.setString(4, request.getParameter("delList") );
					ps.execute();
				} else {
					for (int i=0; i<list.length; i++) {
						if ( list[i].trim().equalsIgnoreCase("") ) {
							continue;
						}
						ps.setString(4, list[i] );
						ps.execute();
					}	
				}
			} else {
				strSQL = "INSERT INTO mproleuser (bssid,subsysid,roleid,userid) VALUES(?,?,?,?)";
				ps = conn.prepareStatement(strSQL);
				ps.setString(1, bssId );
				ps.setString(2, subSysId );
				ps.setInt(3, roleType );
				
				list = request.getParameter("addList").split(wid.splitTokenONE);
				if ( !request.getParameter("addList").contains(wid.splitTokenONE) ) {
					ps.setString(4, request.getParameter("addList") );
					ps.execute();
				} else {
					for (int i=0; i<list.length; i++) {
						if ( list[i].trim().equalsIgnoreCase("") ) {
							continue;
						}
						ps.setString( 4, list[i] );
						ps.execute();
					}	
				}
			}
			res = "OK";
			
		} catch ( SQLException e) {
			wid.kem(conn, ps, e, wid.toEType("SQLException")+errPars, wid.isPrint2Console );
		} catch ( Exception e) {
			wid.kem(conn, ps, e, wid.toEType("Exception")+errPars, wid.isPrint2Console );
		} finally {
			try {
				if ( rs != null ) { rs.close(); }
				if ( ps!= null ) { ps.close(); }
				if ( conn!= null ) { conn.close(); }
			} catch ( SQLException e ) {
				System.out.println( wid.errProcess(e) );
			}
		}
		response.getWriter().write(res);
	}
}